package com.jack.blog.dto;

import com.jack.blog.entity.Blog;
import com.jack.blog.entity.User;
import com.jack.blog.utils.UUIDUtil;
import com.jack.blog.vo.info.UserInfo;
import com.jack.blog.vo.UserVo;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserDto extends BaseDto{

    private void checkUserInfo(UserInfo userInfo)throws IllegalArgumentException{
        MultipartFile avatar = userInfo.getAvatar();
        String type=getFileType(avatar.getOriginalFilename());
        if (!avatar.isEmpty()&&!allowUserAvatar.contains(type))
            throw new IllegalArgumentException("文件类型 ( "+type+" ) 不支持!");
        if (userInfo.getNickName().length()>10)
            throw new IllegalArgumentException("昵称长度超过10!");
        if (userInfo.getIntroduction().length()>50)
            throw new IllegalArgumentException("简介长度超过20!");
    }

    public User UserUpdateToUser(UserInfo userInfo)throws IllegalArgumentException{
        User user=new User();
        checkUserInfo(userInfo);
        if (!userInfo.getAvatar().isEmpty())
            user.setAvatarUrl(UUIDUtil.getUUID()+".jpg");
        user.setNickName(userInfo.getNickName());
        user.setIntroduction(userInfo.getIntroduction());
        return user;
    }

    public UserVo UserToUserVo(User user,List<Blog> blogs){
        UserVo userVo=new UserVo();
        userVo.setId(user.getId());
        userVo.setAvatarUrl(avatarUrl + File.separator +user.getAvatarUrl());
        userVo.setNickName(user.getNickName());
        userVo.setIntroduction(user.getIntroduction());
        int blog=0,browse=0,good=0,bad=0,message=0;
        for (Blog b:blogs){
            blog++;
            browse+=b.getBrowse();
            good+=b.getGood();
            bad+=b.getBad();
            message+=b.getMessage();
        }
        userVo.setBlog(blog);
        userVo.setBrowse(browse);
        userVo.setGood(good);
        userVo.setBad(bad);
        userVo.setMessage(message);
        return userVo;
    }

    public Map<String,String> UserVoToCache(UserVo userVo){
        Map<String,String> map=new HashMap<>();
        map.put("id",String.valueOf(userVo.getId()));
        map.put("nickName",String.valueOf(userVo.getNickName()));
        map.put("avatarUrl",String.valueOf(userVo.getAvatarUrl()));
        map.put("introduction",String.valueOf(userVo.getIntroduction()));
        map.put("blog",String.valueOf(userVo.getBlog()));
        map.put("browse",String.valueOf(userVo.getBrowse()));
        map.put("good",String.valueOf(userVo.getGood()));
        map.put("bad",String.valueOf(userVo.getBad()));
        map.put("message",String.valueOf(userVo.getMessage()));
        return map;
    }

    public UserVo CacheToUserVo(Map<String,String> map){
        UserVo userVo=new UserVo();
        userVo.setId(Integer.valueOf(map.get("id")));
        userVo.setNickName(map.get("nickName"));
        userVo.setAvatarUrl(map.get("avatarUrl"));
        userVo.setIntroduction(map.get("introduction"));
        userVo.setBlog(Integer.valueOf(map.get("blog")));
        userVo.setBrowse(Integer.valueOf(map.get("browse")));
        userVo.setGood(Integer.valueOf(map.get("good")));
        userVo.setBad(Integer.valueOf(map.get("bad")));
        userVo.setMessage(Integer.valueOf(map.get("message")));
        return userVo;
    }
}
